﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace SkypeBot.Commands
{
    using SkypeBot.Core;

    class WeatherCommand : IMessageHandler
    {
        [Message(@"^!w (?<location>\w*)$")]
        public Responce GetCurrentConditions(Message message,string location)
        {
            string strResponce = string.Empty;
            try
            {
                var content = string.Empty;

                using (WebClient mobjWebClient = new WebClient())
                {
                    mobjWebClient.Encoding = Encoding.UTF8;
                    content = mobjWebClient.DownloadString(string.Format("http://api.openweathermap.org/data/2.5/weather?q={0}&mode=json&units=metric", location));
                }

                Conditions objContent = JsonConvert.DeserializeObject<Conditions>(content);
                strResponce = string.Format("Город:{0}\r\nСтрана:{1}\r\nТемпература:{2}\r\nОбщее описание:{3}\r\n", objContent.name, objContent.sys.country, objContent.main.temp, objContent.weather[0].description);

            }
            catch(Exception ex)
            {
                strResponce = "Ты ввел неправильные данные";
            }
            return new Responce(strResponce);
        }



    }

    public class Coord
    {
        public double lon { get; set; }
        public double lat { get; set; }
    }

    public class Sys
    {
        public double message { get; set; }
        public string country { get; set; }
        public int sunrise { get; set; }
        public int sunset { get; set; }
    }

    public class Weather
    {
        public int id { get; set; }
        public string main { get; set; }
        public string description { get; set; }
        public string icon { get; set; }
    }

    public class Main
    {
        public double temp { get; set; }
        public double temp_min { get; set; }
        public double temp_max { get; set; }
        public double pressure { get; set; }
        public double sea_level { get; set; }
        public double grnd_level { get; set; }
        public int humidity { get; set; }
    }

    public class Wind
    {
        public double speed { get; set; }
        public double deg { get; set; }
    }

    public class Clouds
    {
        public int all { get; set; }
    }

    public class Conditions
    {
        public Coord coord { get; set; }
        public Sys sys { get; set; }
        public List<Weather> weather { get; set; }
        public string @base { get; set; }
        public Main main { get; set; }
        public Wind wind { get; set; }
        public Clouds clouds { get; set; }
        public int dt { get; set; }
        public int id { get; set; }
        public string name { get; set; }
        public int cod { get; set; }
    }


}
